package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishInventoryPageQueryDTO;
import com.sky.dto.DishStockDTO;
import com.sky.entity.DishInventory;
import com.sky.mapper.DishInventoryMapper;
import com.sky.result.PageResult;
import com.sky.service.DishInventoryService;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class DishInventoryServiceImpl implements DishInventoryService {

    @Autowired
    private DishInventoryMapper dishInventoryMapper;

    /**
     * 库存分页查询
     * @param dishInventoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishInventoryPageQueryDTO dishInventoryPageQueryDTO) {
        PageHelper.startPage(dishInventoryPageQueryDTO.getPage(),dishInventoryPageQueryDTO.getPageSize());
        Page<DishInventory> page = dishInventoryMapper.pageQuery(dishInventoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 修改库存量
     * @param dishStockDTO
     * @return
     */
    @Override
    public void update(DishStockDTO dishStockDTO) {
        //设置菜品id，库存量，修改时间
        DishInventory dishInventory = DishInventory.builder()
                .dishId(dishStockDTO.getId())
                .stock(dishStockDTO.getStock())
                .updateTime(LocalDateTime.now())
                .build();
        dishInventoryMapper.update(dishInventory);
    }
}
